package cn.itcast.service.system.impl;

import cn.itcast.dao.system.ModuleDao;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;

import cn.itcast.service.utils.RedisUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDao moduleDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public PageInfo<Module> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Module> list = moduleDao.findAll();
        PageInfo<Module> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //查询企业的所有用户
    @Override
    public List<Module> findAll() {

        //从redis缓存中查找
        String moduleListJson = (String) redisUtil.get("moduleList");
        List<Module> moduleList = null;
        try {
            if (moduleListJson==null){//如果redis中不存在，就去数据库中查找
                moduleList = moduleDao.findAll();
                moduleListJson = new ObjectMapper().writeValueAsString(moduleList);
                redisUtil.set("moduleList",moduleListJson);//将从数据库中查到的权限，转为json字符串存储在redis中
            }else {//如果存在，转换为List对象返回
                moduleList = new ObjectMapper().readValue(moduleListJson, List.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return moduleList;

        //return moduleDao.findAll();
    }

    @Override
    public void save(Module module) {
        module.setId(UUID.randomUUID().toString());
        moduleDao.save(module);
    }

    @Override
    public void update(Module module) {
        moduleDao.update(module);
    }

    @Override
    public Module findById(String id) {
        Module module = moduleDao.findById(id);
        return module;
    }

    @Override
    public Boolean delete(String id) {
        moduleDao.delete(id);
        return null;
    }

    //根据角色id查找已经分配的模块
    @Override
    public List<Module> findRoleModuleByRoleId(String roleId) {

        //从redis缓存中查找
        String roleModuleListJson = (String) redisUtil.get("roleModuleList_" + roleId);
        List<Module> roleModuleList = null;
        try {
            if (roleModuleListJson==null){
                roleModuleList = moduleDao.findRoleModuleByRoleId(roleId);
                String key = "roleModuleList_" + roleId;
                String value = new ObjectMapper().writeValueAsString(roleModuleList);
                redisUtil.set(key,value);
            }else {
                roleModuleList = new ObjectMapper().readValue(roleModuleListJson, List.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return roleModuleList;
        //return moduleDao.findRoleModuleByRoleId(roleId);
    }

    //查找用户所拥有的模块
    @Override
    public List<Module> findModuleByUser(User user) {
        Integer degree = user.getDegree();
        List<Module> result = null;
        if (degree==1||degree==0){
            result = moduleDao.findModuleByDegree(degree);
        }else {
            result = moduleDao.findModuleByUserId(user.getId());
        }
        return result;
    }
}
